package contacts.dao.impl;

import java.sql.Connection;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import contacts.dao.IStudentDao;
import contacts.db.DbConnection;
import contacts.po.Room;
import contacts.po.Student;

public class StudentDaoImpl implements IStudentDao{

	@Override
	public void save(Student stu) {
		Connection conn = DbConnection.getConnection();
		PreparedStatement ps = null;
		try {
			String sql = "insert into student values(?,?,?,?,?,?,?,?)";
			ps  = conn.prepareStatement(sql);
			ps.setInt(1, stu.getId());
			ps.setString(2, stu.getsNumber());
			ps.setString(3, stu.getsName());
			ps.setDate(4, stu.getBirthday());
			ps.setString(5, stu.getTel());
			ps.setString(6, stu.getEmail());
			ps.setString(7, stu.getHouseAddress());
			ps.setInt(8,stu.getRoom().getRoomNo());  //
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DbConnection.close(ps, null);
		}
	}
	@Override
	public void update(Student stu) {
		Connection conn=DbConnection.getConnection();
		PreparedStatement ps=null;
		try {
			//student_number这些属性名要一样.
			String sql="update student set student_number=?,student_name=?,birthday=?, tel=?,email=?,house_address=?,s_room_no=? where student_id=?";
			ps  = conn.prepareStatement(sql);
			ps.setString(1, stu.getsNumber());
			ps.setString(2, stu.getsName());
			ps.setDate(3, stu.getBirthday());
			ps.setString(4, stu.getTel());
			ps.setString(5, stu.getEmail());
			ps.setString(6, stu.getHouseAddress());
			ps.setInt(7,stu.getRoom().getRoomNo());
			ps.setInt(8, stu.getId());
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
		
		}
		
	

	@Override
	public Student get(int id) {
		Connection conn=DbConnection.getConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;
		List<Student> studentList=new ArrayList<Student>();
		try {
			//不能用*号
			String sql="select student_id,student_number,student_name,birthday, tel,email,house_address,s_room_no ,room_name from student ,room  where s_room_no=room_no and student_id=?";
			//String sql="select * from student where student_id=? ";
			ps=conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.execute();
			rs=ps.executeQuery();
			while(rs.next()) {
				int id1=rs.getInt(1);
				String sNumber=rs.getString(2);
				String sName=rs.getString(3);
				Date birthday=rs.getDate(4);
				String tel=rs.getString(5);
				String email=rs.getString(6);
				String houseAddress=rs.getString(7);
				
				int roomNo=rs.getInt(8);
				String roomName=rs.getString(9);
				Room room=new Room(roomNo,roomName);
				
				Student stu=new Student(id1,sNumber,sName,birthday,tel,email,houseAddress,room);
				studentList.add(stu);
				
			}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				DbConnection.close(ps, null);
			}
			return studentList.get(0);
			}

	@Override
	public void delete(int id) {
		Connection conn = DbConnection.getConnection();
		PreparedStatement ps = null;
		try {
			String sql = "delete from student where student_id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
		
	}
	@Override
	public List<Student> findAll() {
		Connection conn=DbConnection.getConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;
		List<Student> studentList=new ArrayList<Student>();
		
		try {
			//String sql="select * from student ,room where s_room_no=room_no";
			String sql="select student_id,student_number,student_name,birthday, tel,email,house_address,s_room_no ,room_name from student ,room  where s_room_no=room_no ";
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()) {
				int id=rs.getInt(1);
				String sNumber=rs.getString(2);
				String sName=rs.getString(3);
				Date birthday=rs.getDate(4);
				String tel=rs.getString(5);
				String email=rs.getString(6);
				String houseAddress=rs.getString(7);
				int roomNo=rs.getInt(8);
				String roomName=rs.getString(9);
				Room room=new Room(roomNo,roomName);
				Student stu=new Student(id,sNumber,sName,birthday,tel,email,houseAddress,room);
				studentList.add(stu);
			}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				DbConnection.close(ps, null);
			}
			return studentList;
			}

}
